package com.example.zwh.computershop.commodity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zwh.computershop.R;
import com.example.zwh.computershop.particulars.particulars;
import com.example.zwh.computershop.sqlModel.Product;

import java.util.List;

public class Search_Adapter  extends RecyclerView.Adapter<Search_Adapter.ViewHolder> {

    public List<Product> mProduct;
    static Context se_context;

    static class ViewHolder extends RecyclerView.ViewHolder {

        View linkman;//整个视图
        ImageView image;//图片
        TextView title;//标题
        TextView js;//介绍
        TextView price;//优惠价

        public ViewHolder(View view) {
            super(view);
            image = (ImageView) view.findViewById(R.id.search_home_goods_img);
            title = (TextView) view.findViewById(R.id.search_home_title);
            js = (TextView) view.findViewById(R.id.search_home_js);
            price = (TextView) view.findViewById(R.id.search_home_newprice);
            linkman = view;
        }

    }

    public Search_Adapter(List<Product> products, Context context) {
        mProduct = products;
        se_context = context;
//        Log.e("Shenyutian", products.size()+"进入适配器");
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, final int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_search_item, viewGroup, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.linkman.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewHolder.getAdapterPosition();
                Product product = mProduct.get(position);
                Toast.makeText(v.getContext(), "打开商品" + product.getProductName(), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(se_context, particulars.class);
                intent.putExtra("come_product",product.getProductName());
                se_context.startActivity(intent);
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        //写入数据
        Product product = mProduct.get(i);//拿到数据id_img
        viewHolder.title.setText(product.getProductName());
        //为了图片id getImageID
        int id_img = getImageID("R.drawable." + product.getImgUrl());
        viewHolder.image.setImageResource(id_img);
        viewHolder.js.setText(product.getProductInfor());
        viewHolder.price.setText(Double.toString(product.getPrice()));
    }

    @Override
    public int getItemCount() {
        return mProduct.size();
    }

    /**
     * 根据图片名字反射出图片ID
     * @return
     */
    public static int getImageID(String name){

//        Log.e("Shenyutian", "name: "+name);
        int id = R.drawable.p001;
        int test = 0;
        try {
            test = Integer.valueOf(name.substring(name.length()-7, name.length()-4));
//            Log.e("Shenyutian", "getImageID: "+test );
            id += test-1;
        } catch (Exception e) {
            id = R.drawable.p002;
            e.printStackTrace();
        }
        return id;
    }

}